/**
 * @file 物流信息
 * @author xiaofei
 * @date 2020/7/29
 */
layui.define(['layer', 'form', '$http', 'table', 'Vue'], function (exports) {
    var $ = layui.$;
    var table = layui.table;
    var form = layui.form;
    var $http = layui.$http;
    var opCommonUtils = layui.list_modify_common;

    var getters = {
        getPostList: () => table.cache['orderPostTable']
    };

    var postTable = table.render({
        elem: '#orderPostTable'
        , cols: [[ //标题栏
            { type: 'checkbox', width:'0', hide: true }
            , { field: 'expressCode', title: '快递单号', edit: 'text', width: '15%', align: 'center', edit: 'text', }
            , { field: 'expressCompanyCode', title: '快递公司', width: '30%', templet: '#expressCompanySelect', align: "center" }
            , { field: 'itemTypeName', title: '类型', width: '15%', align: "center" }
            , { field: 'remark', title: '快递备注', edit: 'text', width: '20%', align: "center" }
            , { field: 'statusName', title: '快递状态', width: '10%', align: "center" }
            , { title: '操作', align: "center", toolbar: '#btns', }
        ]]
        , data: orderPostList
        , done: function (res, curr, count) {
            var tableView = this.elem.next(); // 当前表格渲染之后的视图
            layui.each(res.data, function(i, item){
                if (item.modifiable == 1) {
                    tableView.find('tr[data-index=' + i + ']')
                        .css("background-color", "#CCCCCC")
                        .find('td').data('edit', false);
                }
            });

            // 根据已有的值回填下拉框
            layui.each($("select[tag='expressCompanyList']"), function (index, item) {
                var elem = $(item);
                elem.val(elem.data('value'));
            });
            form.render('select');
        }
        , even: false
    });

    table.on('tool(orderPostTable)', function (obj) {
        if (obj.event === 'del') {
            layer.confirm('请确认是否删除当前快递信息？', {
                btn: ['确认','取消']
            }, function (index) {
                getters.getPostList().splice(obj.tr.data('index'), 1); // 根据索引删除当前行
                postTable.reload({
                    data: getters.getPostList()
                });
                layer.close(index);
            }, function (index) {
                layer.close(index);
            });
        }
    });

    // 这是监听下拉框改变
    form.on('select(expressCompanyList)', function (data) {
        var selectElem = $(data.elem);
        var tdElem = selectElem.closest("td");
        var trElem = tdElem.closest("tr");

        var item = getters.getPostList()[trElem.data("index")];
        item.expressCompanyCode = data.value;
        item.expressCompany = selectElem.find("option:selected").text();
    });

    /**
     * 初始化页面事件
     */
    function initEvent() {

        // 新增
        $("#addOrderPost").on("click", function (obj) {
            var newData = {
                expressCode: "",
                expressCompanyCode: "",
                itemTypeName: "新机",
            }
            getters.getPostList().push(newData);
            postTable.reload({
                data: getters.getPostList()
            });
        });

        // 保存
        form.on('submit()', function (data) {
            var postList = [];
            var verify = true;
            getters.getPostList().forEach(item => {
                if (item.expressCompanyCode == '' || item.expressCode == '') {
                    verify = false;
                    return;
                }
                postList.push({
                    expressCode: item.expressCode,
                    expressCompanyCode: item.expressCompanyCode,
                    expressCompany: item.expressCompany,
                    itemType: 1,
                    remark: item.remark || null
                })
            });
            if (!verify) {
                layer.msg("订单号和快递公司为必填项，请检查填写")
                return false;
            }

            data.field.postList = postList;
            console.log(postList);
            $http.lockSave('/work/modify/save/orderPost.action', data.field, data.elem).then(function () {
                layer.msg('操作成功', {icon: 1, time: 1000}, function () {
                    opCommonUtils.goOperateHome();
                });
            }, function (msg) {
                layer.msg(msg);
            });
            return false;
        });
    }

    function init() {
        // 初始化事件
        initEvent();
    }

    init();
    exports("oms_work_modify_orderpost");
});
